package com.subway.babynote;

import java.util.ArrayList;
import java.util.HashMap;

import com.subway.babynote.syncdata.ClientDataRecord.loadinfoRecord;
import com.subway.babynote.syncdata.ClientDataRecord.tipRecord;
import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.database.Cursor;

/*水*/
public class babynote extends Activity {
	/** Called when the activity is first created. */
	NotedbHelper notedb;
	private Handler mHandler = new Handler();
	ListView lv;
	ImageView imageview;
	TextView textview;
	int alpha = 255;
	int b = 0;
	Cursor cur;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.main);

		imageview = (ImageView) this.findViewById(R.id.ImageView01);
		textview = (TextView) this.findViewById(R.id.TextView01);
		lv = (ListView) findViewById(R.id.list);
		lv.setFocusable(false);
		Log.v("BabyNote", "BabyNote start ...");
		imageview.setAlpha(alpha);
		new Thread(new Runnable() {
			public void run() {
				initApp();

				while (b < 2) {
					try {
						if (b == 0) {
							Thread.sleep(1000);
							b = 1;
						} else {
							Thread.sleep(50);
						}
						Log.v("babynote", "start updating");
						updateApp();

					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			}
		}).start();
	//	notedb = new NotedbHelper(this);
		loadinfoRecord lc = syncapi.getSyncImpl().getLoadInfo();
		ArrayList<HashMap<String, Object>> loads = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> loadinfo = new HashMap<String, Object>();
		loadinfo.put("content", lc.content);
		loads.add(loadinfo);
		ListAdapter listAdapter = new SimpleAdapter(this, loads,
				R.layout.list_item, new String[] { "content" },
				new int[] { R.id.list_item });
				
		/*
		 * cur = notedb.getLoadInfo(); ListAdapter listAdapter = new
		 * SimpleCursorAdapter(this, R.layout.list_item, cur, new String[] {
		 * "content" }, new int[] { R.id.list_item });
		 * lv.setAdapter(listAdapter);
		 */
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				imageview.setAlpha(alpha);
				imageview.invalidate();

			}
		};

	}

	public void updateApp() {
		alpha -= 5;

		if (alpha <= 0) {
			b = 2;
			Intent In = new Intent();

			try {

				if (syncapi.getSyncImpl().getUser() != null) {
					In.setClass(babynote.this, maintab.class);
				} else {
					In.setClass(babynote.this, gridviewinputamount.class);
				//	In.setClass(babynote.this, com.subway.babynote.birth.class);
				}

				Log.v("babynote", "before activity");
				startActivity(In);
				this.finish();
			} catch (Exception e) {
				Log.v("babynote", "init babynote e=" + e.getMessage());
			}
		}

		mHandler.sendMessage(mHandler.obtainMessage());

	}

	public void initApp() {
		// notedb = new NotedbHelper(this);
		// notedb.FirstStart();
		// notedb.close();
		syncapi.getSyncImpl().Init(babynote.this);

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.v("babynote", "keycode=" + keyCode);
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			Log.v("BabyNote", "BabyNote end ...");
			return true;
		}
		return false;
	}

	public void onDestroy() {
		cur.close();
		notedb.close();
		syncapi.getSyncImpl().Destroy();
		super.onDestroy();
	}

}
